<?php
namespace app\common\model;

use Exception;

class Code extends Base
{
    protected $name = 'code';

    public function checkCode($phone, $code)
    {
        try{
            $res = $this->getFind(['phone' => $phone, 'is_use' => 0], '*', 'id desc');

            if ($res['code'] != 'ok') {
                return ['code' => 'fail','msg' => "验证码不存在"];
            }

            if ($code != $res['data']['code']) {
                return ['code' => 'fail','msg' => '验证码错误'];
            }

            if (time() > $res['data']['expiration_time']) {
                return ['code' => 'fail','msg' => '验证码已过期'];
            }

            return ['code' => 'ok', 'data' => $res['data']['id']];
        }catch(Exception $e){
            return ['code' => 'fail','msg' => $e->getMessage()];
        }
    }
}